const UserModel = require("../../models/UserModel")

const UserService = {
    login: async (username, password) => {
        return UserModel.find(
            {
                username,
                password
            }
        )
    },
    upload: async (userInfo) => {
        console.log("userInfo");
        console.log(userInfo);
        if (userInfo.avatar) {
            return UserModel.updateOne({
                _id: userInfo._id
            }, {
                ...userInfo
            })
        } else {
            return UserModel.updateOne({
                _id: userInfo._id
            }, {
                username: userInfo.username,
                gender: userInfo.gender,
                introduction: userInfo.introduction
            })
        }
    },
    getAvatar: async (id) => {
        return UserModel.find({
            _id: id
        })
    },
    userCheck: async (username) => {
        return UserModel.find({
            username
        })
    },
    userAdd: async (userInfo) => {
        return UserModel.create({
            ...userInfo
        })
    },
    getList: async () => {
        return UserModel.find()
    },
    userDelete: async (id) => {
        return UserModel.deleteOne({
            _id: id
        })
    },
    userEdit: async (userInfo) => {
        console.log("userInfo");
        console.log(userInfo);
        return UserModel.updateOne({
            _id: userInfo._id
        }, {
            ...userInfo
        })
    }
}

module.exports = UserService